package com.hbsoft.sub;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import com.ant4j.popularize.ui.MainFrame;

public class SysTray 
{
	private MenuItem exitIttem= null;
	private MenuItem logoutItem= null;
	private MainFrame frame = null;	
	
	public SysTray(MainFrame frame)
	{
		this.frame = frame;
		initTray();
		initTrayListener();
	}
	
	//���ϵͳ����
	public void initTray()
	{
		// �жϵ�ǰ����ϵͳ�Ƿ�֧��ϵͳ����
		TrayIcon trayIcon =null;
		if (SystemTray.isSupported())
		{
			// ͨ��̬����getSystemTray()�õ�ϵͳ����
			SystemTray tray = SystemTray.getSystemTray();
			// װ������ͼ��
			Image image = Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/images/c.jpg"));
				
			//Ϊ������̼�һ�������˵�
			PopupMenu popup = new PopupMenu();
			
			//Ϊ������̵ĵ����˵������˵���
			exitIttem = new MenuItem("�˳�");
			logoutItem = new MenuItem("ע��");
		
		
			
	
			
			//�������˵��Ĳ˵�����ӵ����̵����˵���
			
			popup.add(exitIttem);
			popup.add(logoutItem);
			
			// Ϊ������̼�һ����ʾ��Ϣ
			trayIcon = new TrayIcon(image, "����ͨѶ", popup);
			try
			{
				tray.add(trayIcon);
			}
			catch (AWTException e)
			{
				System.err.println("�޷���������������� " + e);
			}
		}
		else
		{
			System.err.println("�޷�ʹ��ϵͳ����!");
		}
		
		//���һ�����ڼ�����
		frame.addWindowListener(new WindowAdapter() 
		{
			public void windowIconified(WindowEvent arg0) 
			{
				frame.dispose();
			}
		});
		
		// Ϊ������̼�һ��������
		trayIcon.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent arg0) 
			{
				if(arg0.getClickCount()==2)
				{
					frame.setExtendedState(JFrame.NORMAL);   
					frame.setVisible(true); 
				}
			}
		});
	}
	
	//�����̲˵�����Ӽ������ķ���
	public void initTrayListener()
	{
		//���˳�ϵͳ�˵�����Ӽ�����
		exitIttem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("aa");
			}
		});
	}
}
